<template>
	<view class="columnbox wdh-100 content">
		<picker class="wdh-100" mode="multiSelector" :range="timeOptions" @change="setInttime">
			<view class="one-info rowbox spb wdh-100">
				<view class="rowbox">
					服务开始时间
				</view>
				<view class="rowbox">
					<view style="margin-right: 10rpx;"> {{inttime}}</view>
					<u-icon name="arrow-right" size="28rpx"></u-icon>
				</view>
			</view>
		</picker>
		<picker class="wdh-100" mode="multiSelector" :range="timeOptions" @change="setEndtime">
			<view class="one-info rowbox spb wdh-100">
				<view class="rowbox">
					服务结束时间
				</view>
				<view class="rowbox">
					<view style="margin-right: 10rpx;"> {{endtime}}</view>
					<u-icon name="arrow-right" size="28rpx"></u-icon>
				</view>
			</view>
		</picker>
		<view style="width: 100%;height: 200rpx;"></view>
		<view class="rowbox wdh-100 bottom-box">
			<view v-if="submitflag" class="rowbox wdh-100 send-btn grey-btn" @click="submitInfo">保存</view>
			<view v-else class="rowbox wdh-100 send-btn" @click="submitInfo">保存</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				inttime: '',
				endtime: '',
				submitflag: false,
				timeOptions: [],
				start: 0,
				end: 0
			}
		},
		computed: {
			user() {
				return this.$store.state.user
			},
			isLogin() {
				return this.$store.state.isLogin
			}
		},
		onLoad() {
			let list = []
			for (var i = 0; i < 24; i++) {
				list.push(i + '')
			}
			this.timeOptions = [
				list,
				[':'],
				['00', '30']
			]
			this.$nextTick(() => {
				this.inttime = this.user.inttime
				this.endtime = this.user.endtime
				this.start = this.user.inttime.split(':')[0] * 1 + (this.user.inttime.split(':')[1] == '30' ? 0.5 :
					0)
				this.end = this.user.endtime.split(':')[0] * 1 + (this.user.endtime.split(':')[1] == '30' ? 0.5 :
					0)
			})
		},
		methods: {
			setInttime(e) {
				let index = e.detail.value
				this.inttime = this.timeOptions[0][index[0]] + ':' + this.timeOptions[2][index[2]]
				let timevalue = this.timeOptions[0][index[0]] * 1 + (this.timeOptions[2][index[2]] == '30' ? 0.5 :
					0)
				if (this.end < timevalue) {
					this.endtime = this.timeOptions[0][index[0]] + ':' + this.timeOptions[2][index[2]]
				}
			},
			setEndtime(e) {
				let index = e.detail.value
				let time = this.timeOptions[0][index[0]] + ':' + this.timeOptions[2][index[2]]
				let timevalue = this.timeOptions[0][index[0]] * 1 + (this.timeOptions[2][index[2]] == '30' ? 0.5 :
					0)
				if (timevalue < this.start) {
					uni.showToast({
						title: '结束时间需大于开始时间',
						icon: 'none'
					})
					return
				}
				this.endtime = time
			},
			submitInfo() {
				if (!this.submitflag) {
					this.submitflag = true
					uni.showLoading({
						title: '正在保存'
					})
					this.$req.post('/doctor/updatedoctortime', {
						doctorid: uni.getStorageSync('uid'),
						inttime: this.inttime,
						endtime: this.endtime
					}).then(res => {
						if (res.data.code == 0) {
							this.getUserDetail()

						} else {
							this.submitflag = false
							uni.hideLoading()
							this.submitflag = false
							uni.showModal({
								title: '保存失败',
								content: res.data.msg
							})
						}
					})
				}
			},
			getUserDetail() {
				this.$req.post('/doctor/doctorinfo', {
					doctorid: uni.getStorageSync('uid')
				}).then(res => {
					let info = res.data.data
					this.$store.dispatch('actionsHandleUserInfoChange', res.data.data);
					this.$store.dispatch('actionsHandleIsLoginChange', true);
					uni.setStorageSync('userinfo', res.data.data)
					uni.hideLoading()
					this.submitflag = false
					uni.showToast({
						title: '保存成功',
						icon: 'success'
					})
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.one-info {
		background: #fff;
		padding: 32rpx 40rpx;
		font-size: 28rpx;
		border-bottom: #F5F8FB solid 1px;

		image {
			width: 50rpx;
			height: 50rpx;
			margin-right: 20rpx;
		}
	}

	.quit-btn {
		width: 600rpx;
		border: #e0e4ea solid 1px;
		height: 90rpx;
		margin-top: 40rpx;
		border-radius: 20rpx;
		font-size: 28rpx;
		color: #00b6b2;
		position: fixed;
		bottom: 70rpx;
	}
</style>